package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.Quotation;
import com.cz.zslu.entity.SysUser;
import com.cz.zslu.entity.ZslStoreCommodity;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.service.IQuotationService;
import com.cz.zslu.service.ISysUserService;
import com.cz.zslu.service.IZslStoreCommodityService;
import com.cz.zslu.service.IZslStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 报价 前端控制器
 * </p>
 *
 * @author 森森
 * @since 2018-12-25
 */
@Api(description = "报价")
@RestController
@RequestMapping("/quotation")
public class QuotationController {
    @Autowired
    private IQuotationService iQuotationService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private IZslStoreCommodityService iZslStoreCommodityService;
    @Autowired
    private IZslStoreService iZslStoreService;


    @ApiOperation("提交报价")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ReturnObject<Quotation> add(@ApiParam("求购商品id") @RequestParam Long commodityId,
                                       @ApiParam("价格") @RequestParam BigDecimal amount) {
        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        Quotation quotation = new Quotation();
        quotation.setCommodityId(commodityId);
        quotation.setAmount(amount);
        quotation.setUserId(BaseContextHandler.getUserID());
        quotation.setStoreId(sysUser.getStoreId());
        iQuotationService.insert(quotation);
        return new ReturnObject(0, "报价成功", quotation);
    }


    @ApiOperation("获取未报价商品")
    @RequestMapping(value = "/geQuotationPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodity>> getMyBuyPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                              @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                              @ApiParam("商品类型（传中文）") String type,
                                                              @ApiParam("通用查询条件") String name) {

        EntityWrapper<ZslStoreCommodity> ew = new EntityWrapper<>();
        if (StringUtils.isNotBlank(type)) {
            ew.like("info.commodity_type", type);
        }
        if (StringUtils.isNotBlank(name)) {
            ew.andNew().like("info.commodity_type", name).or().like("info.name", name).or().like("info.origin", name).or().like(
                    "info.origin_name", name).or().like("info.content", name);
        }
        ew.eq("info.type", 1);
        ew.eq("info.state", 0);
        ew.ne("info.user_id", BaseContextHandler.getUserID());
        //获取已报价商品id
        ew.notIn("info.id", iQuotationService.selectObjs(new EntityWrapper<Quotation>().eq("user_id",
                BaseContextHandler.getUserID()).setSqlSelect("commodity_id")));

        ew.orderBy("info.crt_time", false);
        Page<ZslStoreCommodity> obj = iZslStoreCommodityService.selectPage(new Page<>(offset, limit), ew);
        for (ZslStoreCommodity info : obj.getRecords()) {
            info.setUserUrl(iSysUserService.selectById(info.getUserId()).getPicUrl());
        }
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("获取已报价商品")
    @RequestMapping(value = "/geAlreadyQuotationPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodity>> geAlreadyQuotationPage(
            @ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
            @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
            @ApiParam("商品类型（传中文）") String type,
            @ApiParam("通用查询条件") String name) {

        EntityWrapper<ZslStoreCommodity> ew = new EntityWrapper<>();
        if (StringUtils.isNotBlank(type)) {
            ew.like("info.commodity_type", type);
        }
        if (StringUtils.isNotBlank(name)) {
            ew.andNew().like("info.commodity_type", name).or().like("info.name", name).or().like("info.origin", name).or().like(
                    "info.origin_name", name).or().like("info.content", name);
        }
        ew.eq("info.type", 1);
        List<Object> ids = iQuotationService.selectObjs(new EntityWrapper<Quotation>().eq("user_id",
                BaseContextHandler.getUserID()).setSqlSelect("commodity_id"));
        //获取已报价商品id
        if (ids.size() == 0) {
            ew.eq("info.id", 0);
        } else {
            ew.in("info.id", ids);
        }
        ew.ne("info.user_id", BaseContextHandler.getUserID());

        ew.orderBy("info.crt_time", false);
        Page<ZslStoreCommodity> obj = iZslStoreCommodityService.selectPage(new Page<>(offset, limit), ew);
        for (ZslStoreCommodity info : obj.getRecords()) {
            info.setUserUrl(iSysUserService.selectById(info.getUserId()).getPicUrl());
        }
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("获取商品的所有报价")
    @RequestMapping(value = "/getAllQuotation", method = RequestMethod.POST)
    public ReturnObject<List<Quotation>> getAllQuotation(@ApiParam("商品id") @RequestParam Long commodityId) {
        List<Quotation> list = iQuotationService.selectList(new EntityWrapper<Quotation>().eq("commodity_id",
                commodityId));
        for (Quotation info : list) {
            info.setZslStore(iZslStoreService.selectById(info.getStoreId()));
        }
        return new ReturnObject<>(0, "获取成功", list);
    }

    @ApiOperation("获取商品中我的报价")
    @RequestMapping(value = "/getMyQuotation", method = RequestMethod.POST)
    public ReturnObject<List<Quotation>> getMyQuotation(@ApiParam("商品id") @RequestParam Long commodityId) {
        List<Quotation> list = iQuotationService.selectList(new EntityWrapper<Quotation>().eq("commodity_id",
                commodityId).eq("user_id", BaseContextHandler.getUserID()));
        for (Quotation info : list) {
            info.setZslStore(iZslStoreService.selectById(info.getStoreId()));
        }
        return new ReturnObject<>(0, "获取成功", list);
    }

}

